var userBar = null;
var roleBar = null;
var pwdBar = null;

document.ready(function() {
    userBar = getHtml('writeBar');
    roleBar = getHtml('writeBar2');
    pwdBar = getHtml('writeBar3');
    setHtml('writeBar', '');
    setHtml('writeBar2', '');
    setHtml('writeBar3', '');

    queryKey('ok');
});

function queryKey(flag) {
    openLoad();

    var param = {};
    param.ctrl = 'account';
    param.func = 'getUserList';
    param.isuse = getAjaxValue('isuse');
    param.keyword = getAjaxValue('keyword');

    sendAjax(param, function(result) {
        userList.list = result.userList;
        setHtml('listCount', result.listCount);

        setDisplay('mainBg', '');
        closeLoad();
    });
}

function addUser() {
    setValue('tempid', '');
    setValue('flag', 'add');
    openFloat('600px', 'auto', '70px', lyFloatLeft, userBar);
}

function closeWriteDiv() {
    closeFloat();
}

function editUser(id) {
    setValue('tempid', id);
    setValue('flag', 'edit');

    var param = {};
    param.ctrl = 'account';
    param.func = 'getUserInfo';
    param.userid = getAjaxValue('tempid');

    sendAjax(param, function(result) {
        openFloat('600px', 'auto', '70px', lyFloatLeft, userBar);
        setHtml('writeTitle', '编辑用户');

        setValue('usermc', result.name);
        setValue('username', result.username);

        if (result.isuse == 1)
            getItemByName('userstatus', 0).checked = 'checked';
        else
            getItemByName('userstatus', 1).checked = 'checked';
    });
}

function saveUserInfo() {
    if (isEmpty('usermc') || isWicked('usermc')) {
        showTip('姓名格式错误');
        return;
    }
    if (isEmpty('username') || isWicked('username')) {
        showTip('账号格式错误');
        return;
    }

    var param = {};
    param.ctrl = 'account';
    param.func = 'saveUser';
    param.flag = getAjaxValue('flag');
    param.userid = getAjaxValue('tempid');
    param.usermc = getAjaxValue('usermc');
    param.username = getAjaxValue('username');
    param.status = getItemByName('userstatus').checked ? '1' : '0';

    sendAjax(param, function(result) {
        closeFloat();
        queryKey('ok');
    });
}

function editRole(id, name) {
    setValue('tempid2', id);

    openFloat('600px', 'auto', '70px', lyFloatLeft, roleBar);
    setHtml('writeTitle2', '设置角色权限：' + name);
}

function closeWriteDiv2() {
    closeFloat();
}

function getRoleOfType() {
    if (isEmpty('roletype')) {
        setHtml('roleid', '');
        return;
    }

    var param = {};
    param.ctrl = 'account';
    param.func = 'getRoleType';
    param.typeid = getAjaxValue('roletype');

    sendAjax(param, function(result) {
        if (result.res == 1) {
            setHtml('roleid', '');
            setSelectItem('roleid', [{'text':'请选择', 'value':''}]);
            setSelectItem('roleid', result.roleList);
        }
        else {
            setHtml('roleid', '');
            setSelectItem('roleid', [{'text':'请选择', 'value':''}]);
        }
    }, true);
}

function saveRoleInfo() {
    if (isEmpty('tempid2')) {
        showTip('ID格式错误');
        return;
    }
    if (isEmpty('roletype') || !isNum('roletype')) {
        showTip('角色类型格式错误');
        return;
    }
    if (isEmpty('roleid')) {
        showTip('角色名称格式错误');
        return;
    }

    var param = {};
    param.ctrl = 'account';
    param.func = 'editRole';
    param.userid = getAjaxValue('tempid2');
    param.roleid = getAjaxValue('roleid');

    sendAjax(param, function(result) {
        closeFloat();
        queryKey('ok');
    });
}

function resetUsername(id, name) {
    setValue('tempid', id);
    
    openConfirm('确定重置 【' + name + '】 的账号吗？', tipleft, function() {
        var param = {};
        param.ctrl = 'account';
        param.func = 'retUsername';
        param.userid = getAjaxValue('tempid');

        sendAjax(param, function(result) {
            queryKey('ok');
        });
    });
}

function resetPassword(id, name) {
    setValue('tempid3', id);

    openFloat('600px', 'auto', '70px', lyFloatLeft, pwdBar);
    setHtml('writeTitle3', '重置密码：' + name);
}

function closeWriteDiv3() {
    closeFloat();
}

function savePasswordInfo() {
    if (isEmpty('tempid3')) {
        showTip('ID格式错误');
        return;
    }
    if (isEmpty('password') || isWicked('password')) {
        showTip('新密码格式错误');
        return;
    }

    var param = {};
    param.ctrl = 'account';
    param.func = 'retPassword';
    param.userid = getAjaxValue('tempid3');
    param.password = getAjaxValue('password');

    sendAjax(param, function(result) {
        closeFloat();
        queryKey('ok');
    });
}